/**    
 * @{#} MyDoctorAdapter.java Create on 2014-6-18 下午4:30:10    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import cn.o.app.event.listener.OBitmapLoadCallBack;
import cn.o.app.ui.OImageView;
import cn.skinapp.R;

import com.evan.common.adapter.BaseListAdapter;
import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.lidroid.xutils.BitmapUtils;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;

public class FollowDoctorAdapter extends BaseListAdapter implements
		OnClickListener {

	HashMap<String, Object> params = new HashMap<String, Object>();

	protected BitmapUtils mBitmapUtils;

	/**
	 * @param activity
	 * @param array
	 */
	public FollowDoctorAdapter(Activity activity, ArrayList<Object> array) {
		super(activity, array);
		mBitmapUtils = new BitmapUtils(activity);
		params.put(Constant.PARAM_API, "Follow.Set");
		params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);
	}

	class ViewHolder {
		OImageView img;
		TextView text_doctor_name, text_doctor_skill, text_doctor_hospital;
		Button btn_guanzhu;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.evan.common.adapter.BaseAdapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (CommonUtility.isNull(convertView)) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_doctor, null);
			viewHolder.img = (OImageView) convertView
					.findViewById(R.id.imgLayout_show);
			viewHolder.text_doctor_name = (TextView) convertView
					.findViewById(R.id.text_doctor_name);
			viewHolder.text_doctor_skill = (TextView) convertView
					.findViewById(R.id.text_doctor_skill);
			viewHolder.btn_guanzhu = (Button) convertView
					.findViewById(R.id.btn_guanzhu);
			viewHolder.text_doctor_hospital = (TextView) convertView
					.findViewById(R.id.text_doctor_hospital);
			CommonUtility.setViewHolderTag(convertView, viewHolder);
		} else {
			viewHolder = (ViewHolder) CommonUtility
					.getViewHolderTag(convertView);
		}

		// viewHolder.btn_guanzhu.setVisibility(View.VISIBLE);

		JSONObject doctor = (JSONObject) getItem(position);
		try {
			convertView.setTag(doctor);
			String iconPath = doctor.getString("Pic");
			viewHolder.text_doctor_name.setText(doctor.getString("Name"));
			if (iconPath.indexOf(".") == -1) {
				iconPath += iconPath + Constant.IMG_TH;
			}
			mBitmapUtils.display(viewHolder.img, GlobalSettings.SERVER_IMG_URL
					+ iconPath, new OBitmapLoadCallBack<View>());
			boolean isDoctor = doctor.getInt("User_Type") == 2;
			if (isDoctor) {
				viewHolder.text_doctor_skill.setVisibility(View.VISIBLE);
				StringBuilder sb = new StringBuilder();
				sb.append("（");
				JSONArray professional = doctor.getJSONArray("Professional");
				sb.append(CommonUtility.convertJSONArray2String(professional,
						","));
				sb.append(" | ");
				sb.append(doctor.getString("Hospital"));
				sb.append("）");
				viewHolder.text_doctor_hospital.setText(sb);
				viewHolder.text_doctor_skill.setText("专长："
						+ CommonUtility.convertJSONArray2String(
								doctor.getJSONArray("Skilled"), " | "));
			} else {
				StringBuilder sb = new StringBuilder();
				String sex = CommonUtility.sex.get(doctor.getInt("Sex"));
				String age = CommonUtility.getAgeByBirthday(doctor
						.getString("Birth"));
				String nation = doctor.getString("Nation");
				String address = doctor.getString("City");
				String marry = CommonUtility.marry.get(doctor
						.getInt("Marital_Status"));
				sb.append(sex);
				if (!CommonUtility.isNull(age)) {
					sb.append("，");
				}
				sb.append(age);
				if (!CommonUtility.isNull(nation)) {
					sb.append("，");
				}
				sb.append(nation);
				if (!CommonUtility.isNull(address)) {
					sb.append("，");
				}
				sb.append(address);
				if (!CommonUtility.isNull(marry)) {
					sb.append("，");
				}
				sb.append(marry);
				viewHolder.text_doctor_skill.setText(sb);
			}

			viewHolder.btn_guanzhu.setText("取消关注");
			viewHolder.btn_guanzhu.setTag(doctor.getString("Uid"));
			viewHolder.btn_guanzhu.setTag(R.id.tag_obj, position);
			viewHolder.btn_guanzhu.setTag(R.id.tag_obj1, convertView);
			viewHolder.btn_guanzhu.setOnClickListener(this);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		CommonUtility.setListViewItemBG(convertView, position);
		return convertView;
	}

	int operate = 0;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(final View view) {
		// TODO Auto-generated method stub
		final Button btn_guanzhu = (Button) view;
		String uid = (String) btn_guanzhu.getTag();
		final int position = (Integer) btn_guanzhu.getTag(R.id.tag_obj);
		params.put("uids", uid);
		params.put("method", "cancle");
		operate = 0;
		RequestTaskIntercept.requestIntercept(activity, Constant.URL.REQUEST,
				new RequestHandler(activity, new HandlerCallback() {

					@Override
					public void callback(Object o) {
						// TODO Auto-generated method stub
						if (!CommonUtility.isNull(o)) {
							JSONObject object = (JSONObject) o;
							JSONObject oo = (JSONObject) getItem(position);
							View convertView = (View) view
									.getTag(R.id.tag_obj1);
							if (CommonUtility.response200(object)) {
								if (operate == 0) {
									CommonUtility.toast(activity, "取消关注成功");
								}
								Intent data = new Intent(
										Constant.RECIVER_QUESTION);
								try {
									data.putExtra(Constant.IDENTITY_KEY,
											oo.getInt("Uid"));
								} catch (Exception e) {
									// TODO: handle exception
									e.printStackTrace();
								}
								data.putExtra(Constant.IDENTITY_KEY_1, operate);
								activity.sendBroadcast(data);
								list.remove(position);
								notifyUpdate();
							} else {
								CommonUtility.toast(activity, "操作失败");
							}
							convertView.setTag(oo);
						}
					}
				}), params);
	}
}
